var select_node =null;
$(function(){
	
	/**项目指标树*/
	var _pi_tree = $('#pi_tree');
	if(_pi_tree) {
		$('#pi_tree').tree({
		    url : 'projectIndex/projectIndexs',
		    onLoadSuccess:function(node,data){
		         var t = $(this);
		  		 if(data){
				     var roots = t.tree('getRoots');
				     if (roots.length > 0) {
				    	 $(roots).each(function(index,root){
				    		 // console.log(root);
				    		 t.tree('expand', root.target);
			        	 });
				     }
				}
		    },
		    onClick : function (node) {
		    	//console.log(node);
		    	select_node = node;
		    	$('#task_dg').datagrid('reload', {
		    		projectIndexId : node.id,
		    		actorId : $('#userId').val()
		    	});
		    }
		});
	}
	//-------------init taskDatagrid-----------------------------------------------------
	$('#task_dg').datagrid(
			{
				border : 0,
				nowrap : true,
				striped : true,
				fit : true,// 自动大小
				url : 'task/queryList',
				queryParams : {actorId : $('#userId').val()},
				frozenColumns : [ [ 
				     {field : 'id',	title : 'ID',width : 80},
				     {field : 'name',	title : '任务主题',width : 200}
				]],
				columns : [ [
				    {field: "startTime", title:'计划时间',width:160 ,
						formatter : function (value, row, index){
							return formatTaskTimeAdv(value, row);
						}
				    },{field: "status", title:'审批状态',width:100 ,
						formatter : function (value, row, index){
							return formateTaskStatusAdv(value, row);
						}
				    },{field: "planAmount", title:'预算金额',width:100 ,
						formatter : function (value, row, index){
							return formateAmount(value, row);
						}
				    },{field: "factAmount", title:'实际使用资金',width:100 ,
						formatter : function (value, row, index){
							return formateAmount(value, row);
						}
				    },{field: "progress", title:'总体进度',width:60 ,
						formatter : function (value, row, index){
							return formateProgress(value, row);
						}
				    },{field: "actorName", title:'负责人',width:150 }
				    ,{field: "actorId", title:'负责人',width:150 ,hidden : true},
				    {field: "taskActorIds", title:'相关参与人',width:150 ,hidden : true}
				    ,{field: "taskActorNames", title:'相关参与人',width:150 ,
						formatter : function (value, row, index){
							return displayTaskActor(value, row);
						}
				    }
				             ]],
				             singleSelect : true,
								rownumbers : true,
								pagination : true,
								pageSize : 20,
								onClickRow : function(rowIndex, rowData) {
									$('#taskActorNames').val(rowData.taskActorNames);
									$('#taskActorIds').val(rowData.taskActorIds);
									$('#name').val(rowData.name);
									var options = $('#taskPlan_dg').datagrid('options');
								    options.url = 'taskPlan/queryTaskPlans';
								    //$('#taskPlan_dg').datagrid(options);
									$('#taskPlan_dg').datagrid('reload', {
							    		taskId : rowData.id
							    	});
								}
							}).datagrid('getPager').pagination({
						pageSize : 20,// 每页显示的记录条数，默认为10
						pageList : [ 20, 50, 100 ],// 可以设置每页记录条数的列表
						beforePageText : '第',// 页数文本框前显示的汉字
						afterPageText : '页    共 {pages} 页',
						displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录'
					});
	
	//------------taskDatagrid init END-------------------------------------------------
	//-----------------------------------------------------------------------------------
	$('#taskPlan_dg').datagrid(
			{
				border : 0,
				nowrap : true,
				striped : true,
				fit : true,// 自动大小
				url:'',
				//url : 'taskPlan/queryTaskPlans',
				frozenColumns : [ [ {
					field : 'id',
					title : 'ID',
					hidden : true
				}, {
					field : 'taskId',
					title : '任务ID',
					hidden : true
				}, {
					field : 'name',
					title : '名称',
					width : 230,
					formatter : function(value, row, index) {
						if (row.isTimeout == 1) {
							if (row.status == 0)
								return colorRow(value, "red");
							return colorRow(value, "blue");
						}
						return value;

					}
				}, {
					field : 'planActorName',
					title : '负责人',
					width : 100,
					formatter : function(value, row, index) {
						if (row.isTimeout == 1) {
							if (row.status == 0)
								return colorRow(value, "red");
							return colorRow(value, "blue");
						}
						return value;

					}
				} ] ],
				columns : [ [
						{
							field : 'endTime',
							title : '计划完成时间',
							width : 150,
							formatter : function(value, row, index) {
								if (row.isTimeout == 1) {
									if (row.status == 0)
										return colorRow(value, "red");
									return colorRow(value, "blue");
								}
								return value;

							}
						},
						{
							field : 'finishTime',
							title : '实际完成时间',
							width : 150,
							formatter : function(value, row, index) {
								var _value = value || '';
								if (row.isTimeout == 1) {
									if (row.status == 0)
										return colorRow(_value, "red");
									return colorRow(_value, "blue");
								}
								return _value;

							}
						},
						{
							field : 'status',
							title : '完成情况',
							width : 100,
							formatter : function(value, row, index) {
								var _value = value || '';
								if (value == 0) {
									_value = "未完成";
								}
								if (value == 1) {
									_value = "已完成";
								}
								if (row.isTimeout == 1) {
									if (row.status == 0)
										return colorRow(_value, "red");
									return colorRow(_value, "blue");
								}
								return _value;
							}
						},
						{
							field : 'isTimeout',
							title : '超时状态',
							width : 100,
							formatter : function(value, row, index) {
								var _value = value || '';
								if (_value == 1) {
									_value = "超时";
								} else {
									_value = "正常";
								}
								if (row.isTimeout == 1) {
									if (row.status == 0)
										return colorRow(_value, "red");
									return colorRow(_value, "blue");
								}
								return _value;
							}
						},
						{
							field : 'planAmount',
							title : '预算',
							width : 100,
							formatter : function(value, row, index) {
								if (row.isTimeout == 1) {
									if (row.status == 0)
										return colorRow(value, "red");
									return colorRow(value, "blue");
								}
								return value;

							}
						},
						{
							field : 'remark',
							title : '描述',
							width : 300,
							formatter : function(value, row, index) {
								if (row.isTimeout == 1) {
									if (row.status == 0)
										return colorRow(value, "red");
									return colorRow(value, "blue");
								}
								return value;

							}
						},
						{
							field : 'expectResult',
							title : '预期成果',
							width : 300,
							formatter : function(value, row, index) {
								if (row.isTimeout == 1) {
									if (row.status == 0)
										return colorRow(value, "red");
									return colorRow(value, "blue");
								}
								return value;

							}
						}] ],
				singleSelect : true,
				rownumbers : true,
				toolbar : '#tb',
				pagination : true,
				pageSize : 20,
				onClickRow : function(rowIndex, rowData) {
				}
			}).datagrid('getPager').pagination({
		pageSize : 20,// 每页显示的记录条数，默认为10
		pageList : [ 20, 50, 100 ],// 可以设置每页记录条数的列表
		beforePageText : '第',// 页数文本框前显示的汉字
		afterPageText : '页    共 {pages} 页',
		displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录'
	});
	//---------------------END INIT TASKPLAN TABLE-----------------------------------------
	function colorRow(value, color) {
		if (value == null || value == '')
			return value;
		return "<font color='" + color + "'>" + value + "</font>";
	}
	
	/** 新增计划 */
	$('#addPlan').click(function(){
		var row = $('#task_dg').datagrid('getSelected');
		if(row){
		var _url = 'taskPlan/editTaskPlan.shtml?taskId='+row.id;
		showEasyUiWindow('contentFrame', _url, 'win', '新增任务计划', 580, 450);
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
	
	/*** 编辑计划 */
	$('#editPlan').click(function(){
		var row = $('#taskPlan_dg').datagrid('getSelected');
		if (row) {
			var url = 'taskPlan/editTaskPlan.shtml?id=' + row.id;
			showEasyUiWindow('contentFrame', url, 'win', '编辑任务计划', 580, 450);
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
	
	/**查看*/
	$('#detailPlan').click(function(){
		var row = $('#taskPlan_dg').datagrid('getSelected');
		if (row) {
			var url = 'taskPlan/detailTaskPlan.shtml?id=' + row.id;
			showEasyUiWindow('contentFrame', url, 'win', '查看任务计划详情',580, 450);
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
	
	/*** 删除 */
	$('#removePlan').click(function(){
		var row = $('#taskPlan_dg').datagrid('getSelected');
		if (row) {
			$.messager.confirm('确认', '你确认要删除此条数据吗?', function(r) {
				if (r) {
					var id = row.id;				
					$.get('taskPlan/deleteTaskPlan.shtml', {id : id}, function(rs) {
						if (rs.success == true) {
							$.messager.alert('warning', '删除成功', 'warning', function(){
								dealTaskPlan('remove', id);
							});
						} else {
							$.messager.alert('warning', '删除失败，请联系管理员', 'warning');
						}
					});
				}
			});
		} else {
			$.messager.alert('warning', '请选择一条数据！', 'warning');
		}
	});
})